/**
 * Created by yuanyue on 2014-08-15.
 */
package com.eastcom.groovy.task.taskdirector.simple
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader
import org.springframework.core.io.FileSystemResource

import com.eastcom.groovy.config.GroovyScript
import com.eastcom.groovy.config.TaskConfig
import com.eastcom.groovy.context.ServerContext
import com.eastcom.groovy.context.TaskContext
import com.eastcom.groovy.provider.DbContextProvider
import com.eastcom.groovy.provider.DynamicBeanRegisterProvider
import com.eastcom.groovy.provider.SpringContextProvider
import com.eastcom.groovy.task.Task
import com.eastcom.groovy.tools.db.DbEngine
import com.eastcom.groovy.tools.db.DbInfo
import com.eastcom.groovy.tools.db.Table
import com.eastcom.groovy.tools.file.GroovyUtil
import com.eastcom.groovy.tools.file.JaxbUtil
import com.eastcom.groovy.tools.TaskUtil
import com.eastcom.groovy.tools.db.DbEngine

import javax.xml.bind.annotation.XmlAccessType
import javax.xml.bind.annotation.XmlAccessorType
import javax.xml.bind.annotation.XmlElement
import javax.xml.bind.annotation.XmlRootElement
import javax.xml.bind.annotation.XmlTransient

import java.sql.CallableStatement
import java.sql.Types
import java.text.ParseException
import java.util.Map;
import java.util.regex.Matcher
import java.util.regex.Pattern

DbEngine dbEngine;
/**
 * Created by yuanyue on 2014-08-13.
 */
/**
 * 执行方法
 * @param jobId
 */

public boolean initJob(String jobId){
	BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);

	reader.loadBeanDefinitions(new FileSystemResource("E:\\workspace2\\GreatDataCollector\\config\\db_simplesql_collector\\dao_config.xml"));

	BeanFactory bf=(BeanFactory)reg;
	println bf.getBean("oracle_jiangsu_nhm")
	TaskContext.taskConfig.put(jobId+"_"+"dao", reg)
}

public boolean doJob(String jobId) {
	BeanFactory bf = (BeanFactory)TaskContext.taskConfig.get(jobId+"_"+"dao")

	DbEngine dbEngine = SpringContextProvider.getBean("dbEngine")
	dbEngine.getJdbcUtil().setDataSource(bf.getBean("oracle_jiangsu_nhm"))
	BufferedReader reader = null;
	StringBuilder sqls = new StringBuilder()
	try {
		reader = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\程序\\数据\\insert_sql4.sql"), "GBK"));
	} catch (UnsupportedEncodingException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	} catch (FileNotFoundException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}
	String line = null;
	boolean isFileEnd = false;
	int counts = 0
	boolean rs = false
	while(true){
		line = reader.readLine();
		if (line == null) {
			break
		}
		sqls.append(line)
		if(line.contains(");")){
			String sql = sqls.toString()
			try {
				dbEngine.jdbcUtil.execute(sql.substring(0, sql.length()-1))
			} catch (Exception e) {
				e.printStackTrace()
			}
			
			sqls = new StringBuilder();
		}
	}
//	String sql = "SELECT * FROM CMNETTV_CELL_USER_COUNT t where t.cellid=311893"
//	//可以对SQL拼接，赋值等各种动作
//	//....
//	//数据库名字，就是dao_config.xml中的数据源Key
//	//	String dbName = "oracle_39"
//	//	ServerContext.setCustomerType(dbName)
//	println dbEngine.jdbcUtil.queryForList(sql).get(0).get("CELLNAME")
	println "目的资源处理完成!"
	println "任务" + jobId + "完成！"
	println "wodkjfsdskfsdkfsdk"
	return true
}


